package com.yichen.rent.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.yichen.rent.dao.RentDao;
import com.yichen.rent.pojo.Rent;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class RentService {

	@Autowired
	private RentDao rentDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Rent> findAll() {
		return rentDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Rent> findSearch(Map whereMap, int page, int size) {
		Specification<Rent> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return rentDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Rent> findSearch(Map whereMap) {
		Specification<Rent> specification = createSpecification(whereMap);
		return rentDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Rent findById(String id) {
		return rentDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param rent
	 */
	public void add(Rent rent) {
		rent.setId( idWorker.nextId()+"" );
		rentDao.save(rent);
	}

	/**
	 * 修改
	 * @param rent
	 */
	public void update(Rent rent) {
		rentDao.save(rent);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		rentDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Rent> createSpecification(Map searchMap) {

		return new Specification<Rent>() {

			@Override
			public Predicate toPredicate(Root<Rent> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.equal(root.get("id").as(String.class), searchMap.get("id")));
                }
                // 姓名
                if (searchMap.get("house_id")!=null && !"".equals(searchMap.get("house_id"))) {
                	predicateList.add(cb.equal(root.get("house_id").as(String.class), searchMap.get("house_id")));
                }
                // 性别
                if (searchMap.get("request_date")!=null && !"".equals(searchMap.get("request_date"))) {
                	predicateList.add(cb.like(root.get("request_date").as(String.class), "%"+(String)searchMap.get("request_date")+"%"));
                }
                // 手机
                if (searchMap.get("bname")!=null && !"".equals(searchMap.get("bname"))) {
                	predicateList.add(cb.like(root.get("bname").as(String.class), "%"+(String)searchMap.get("bname")+"%"));
                }
                // 身份证
                if (searchMap.get("id_card")!=null && !"".equals(searchMap.get("id_card"))) {
                	predicateList.add(cb.like(root.get("id_card").as(String.class), "%"+(String)searchMap.get("id_card")+"%"));
                }
                // 职业
                if (searchMap.get("occupation")!=null && !"".equals(searchMap.get("occupation"))) {
                	predicateList.add(cb.like(root.get("occupation").as(String.class), "%"+(String)searchMap.get("occupation")+"%"));
                }
                // 学历
                if (searchMap.get("education")!=null && !"".equals(searchMap.get("education"))) {
                	predicateList.add(cb.like(root.get("education").as(String.class), "%"+(String)searchMap.get("education")+"%"));
                }
                // 是否认证
                if (searchMap.get("authentication")!=null && !"".equals(searchMap.get("authentication"))) {
                	predicateList.add(cb.like(root.get("authentication").as(String.class), "%"+(String)searchMap.get("authentication")+"%"));
                }
                // 备注
                if (searchMap.get("remark")!=null && !"".equals(searchMap.get("remark"))) {
                	predicateList.add(cb.like(root.get("remark").as(String.class), "%"+(String)searchMap.get("remark")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
